<template>
  <div class="admin-layout">
    <!-- 左侧导航栏 -->
    <aside class="sidebar">
      <div class="sidebar-header">
        <i class="fas fa-tools"></i>
        <span>后台管理</span>
      </div>

      <ul class="nav-list">
        <li>
          <router-link to="/admin/dashboard" active-class="active">
            <i class="fas fa-chart-line"></i> 控制台
          </router-link>
        </li>
        <li>
          <router-link to="/admin/articles" active-class="active">
            <i class="fas fa-file-alt"></i> 文章管理
          </router-link>
        </li>
        <li>
          <router-link to="/admin/comments" active-class="active">
            <i class="fas fa-comments"></i> 评论管理
          </router-link>
        </li>
        <li>
          <router-link to="/admin/users" active-class="active">
            <i class="fas fa-users"></i> 用户管理
          </router-link>
        </li>
      </ul>
    </aside>

    <!-- 右侧主内容 -->
    <main class="main-content">
      <header class="admin-header">
        <h2>{{ currentTitle }}</h2>
        <router-link to="/" class="back-home" replace @click.native="goHome">
          <i class="fas fa-home"></i> 返回前台
        </router-link>
      </header>

      <section class="admin-body">
        <!-- 子路由出口 -->
        <router-view />
      </section>
    </main>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import { useRouter } from 'vue-router'
const router = useRouter()

const goHome = (e: Event) => {
  e.preventDefault()
  router.push('/').then(() => {
    // 强制刷新页面，让前台布局生效
    window.location.reload()
  })
}
const route = useRoute()

// 根据当前路由动态显示标题
const currentTitle = computed(() => {
  switch (route.name) {
    case 'admin-dashboard': return '控制台'
    case 'admin-articles': return '文章管理'
    case 'admin-comments': return '评论管理'
    case 'admin-users': return '用户管理'
    default: return '后台管理'
  }
})
</script>

<style scoped>
.admin-layout {
  display: flex;
  height: 100vh;
  background: #f5f6fa;
}

/* 左侧导航栏 */
.sidebar {
  width: 230px;
  background: #2d3436;
  color: white;
  display: flex;
  flex-direction: column;
  padding-top: 20px;
  box-shadow: 2px 0 8px rgba(0,0,0,0.15);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 30px;
  gap: 8px;
}

.nav-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.nav-list li {
  margin: 10px 0;
}

.nav-list a {
  display: flex;
  align-items: center;
  padding: 12px 20px;
  color: #dfe6e9;
  text-decoration: none;
  font-size: 15px;
  transition: background 0.3s, color 0.3s;
}

.nav-list a i {
  margin-right: 10px;
}

.nav-list a:hover {
  background: #636e72;
  color: #fff;
}

.nav-list a.active {
  background: #fc5531;
  color: white;
}

/* 主内容区 */
.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.admin-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: white;
  padding: 15px 25px;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}

.admin-header h2 {
  margin: 0;
  color: #333;
  font-size: 20px;
}

.back-home {
  color: #fc5531;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s;
}

.back-home:hover {
  color: #e04a28;
}

.admin-body {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background: #f9f9f9;
}
</style>
